package com.imau.controller;

import com.imau.bean.Permission;
import com.imau.common.entiry.ResultCode;
import com.imau.common.entiry.ResultEntity;
import com.imau.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RequestMapping("/permission")
@RestController
public class PermissionController {


    @Autowired
    private PermissionService permissionService;


    @PostMapping
    public ResultEntity add(Map map) {


        int row = permissionService.savePermission(map);


        return ResultEntity.writeResult(row > 0 ? true : false);


    }


    @DeleteMapping("/{id}")
    public ResultEntity removeById(@PathVariable String id) {


        //删除
        int row = permissionService.deleteByPermissionId(id);
        //4.返回响应
        return ResultEntity.writeResult(row > 0 ? true : false);


    }


    @PutMapping
    public ResultEntity edit(@RequestBody Map map) {


        //3.保存
        int row = permissionService.updatePermission(map);

        //4.返回响应

        return ResultEntity.writeResult(row > 0 ? true : false);


    }


    @PostMapping("/list")

    public ResultEntity list(@RequestBody Permission permission) {


        //3.保存

        List<Permission> allPermission = permissionService.findAllPermission(permission);


        return new ResultEntity(ResultCode.SUCCESS, allPermission);


    }


    @GetMapping("/{id}")

    public ResultEntity getInfo(@PathVariable String id) throws ServletException, IOException {


        Map<String, Object> permissionMap = permissionService.findByPermissionId(id);


        return new ResultEntity(ResultCode.SUCCESS, permissionMap);


    }
}
